package com.serkol.ams.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.bean.Result;
import com.serkol.ams.entity.LoginLog;
import com.serkol.ams.entity.VideoCategory;
import com.serkol.ams.entity.VideoDetail;
import com.serkol.ams.entity.VideoMaster;
import com.serkol.ams.service.VideoCategoryService;
import com.serkol.ams.service.VideoDetailService;
import com.serkol.ams.service.VideoMasterService;
import com.serkol.annotation.IsLogin;
import com.serkol.constant.AuthConstant;
import com.serkol.constant.ResultConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/video")
@CacheConfig(cacheNames = "video")
public class VideoApiController {
    @Autowired
    private VideoMasterService masterService;
    @Autowired
    private VideoDetailService detailService;

    @Autowired
    private VideoCategoryService categoryService;

    @Value("${upload.host}")
    private String uploadhost;

    @IsLogin
    @GetMapping("/findByPageDetails")
    public Result findByPageDetails(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize, @RequestParam(name = "current", defaultValue = "0") int current,
                                    @RequestParam(name = "masterId", defaultValue = "0") long masterId) {
        AuthConstant.setPathUrl(uploadhost);
        Result result = new Result();
        MyPageOld list = detailService.findByList(pageSize,current, masterId);
        if (list.getSize() > 0) {
            result.setStatus(ResultConstant.SUCSESS);
            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }
        return result;
    }

    @IsLogin
    @GetMapping("/findByDetails")
    public Result findByDetails(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize, @RequestParam(name = "current", defaultValue = "0") int current
                                ) {
        AuthConstant.setPathUrl(uploadhost);
        Result result = new Result();
        MyPageOld list = detailService.findByPlayList(pageSize, current);
        if (list.getSize() > 0) {
            result.setStatus(ResultConstant.SUCSESS);
            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }
        return result;
    }

    @IsLogin
    @GetMapping("/findByPageList")
    public Result findByPageList(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize, @RequestParam(name = "current", defaultValue = "0") int current) {
        AuthConstant.setPathUrl(uploadhost);
        Result result = new Result();
        MyPageOld list = masterService.findByList(pageSize, current);
        if (list.getSize() > 0) {
            result.setStatus(ResultConstant.SUCSESS);
            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }
        return result;
    }


    @IsLogin
    //   @RedisCacheRemove(key = "goods::home::findByGoodsAngent",isLang = true,isParam = true)
    @RequestMapping("edit")
    public Result edit(@RequestBody VideoMaster data) {
        //  VideoMaster data = object.toJavaObject(VideoMaster.class);
        System.out.println(data);
        boolean b = false;
        if (data.getId() != null && data.getId()>0) {
            b = masterService.updateById(data);
        } else {
            b = masterService.save(data);
        }

        return b ? Result.ok() : Result.failure(ResultConstant.FAIL);
    }


    @IsLogin
    //   @RedisCacheRemove(key = "goods::home::findByGoodsAngent",isLang = true,isParam = true)
    @RequestMapping("editDetails")
    public Result editDetails(@RequestBody VideoDetail data) {
        //  VideoMaster data = object.toJavaObject(VideoMaster.class);
        System.out.println(data);
        boolean b = false;
        if (data.getId() != null && data.getId()>0) {
            b = detailService.updateById(data);
        } else {
            b = detailService.save(data);
        }

        return b ? Result.ok() : Result.failure(ResultConstant.FAIL);
    }

    @IsLogin
    @GetMapping("/findByCategoryList")
    public Result findByCategoryList() {
        //     AuthConstant.setPathUrl(uploadhost);
        Result result = new Result();
        List<Map<String, Object>> list = categoryService.findByList();
        if (!list.isEmpty()) {
            result.setStatus(ResultConstant.SUCSESS);
            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }
        return result;
    }


    @IsLogin
    @GetMapping("/findByCategoryDataList")
    public Result findByCategoryDataList() {
        LoginLog loginLog = AuthConstant.getLoginLog();


        QueryWrapper<VideoCategory> query = new QueryWrapper<>();


        List<VideoCategory> list = categoryService.list(query);
        return Result.ok(list);
    }


    @GetMapping("/findByNewList")
    //W   @RedisCache(key = "video::home::findByNewList",isLang = true,isParam = true)
    public Result findByNewList() {
        AuthConstant.setPathUrl(uploadhost);

        List<Map<String, Object>> list = masterService.findByNewList();
        return Result.ok(list);
    }
    @IsLogin
    @GetMapping("/findByHomeList")
    //  @Cacheable(cacheNames = "video::home", key = "'findByHomeist'")
    //  @RedisCache(key = "video::home::findByHomeist",isLang = true,isAuth = true,isAgent = true)
    public Result findByHomeList() {
        AuthConstant.setPathUrl(uploadhost);

        List<Map<String, Object>> list = masterService.findByHomeList();
        return Result.ok(list);
    }


    @IsLogin
    @GetMapping("/findByHomePageList")
    public Result findByHomePageList(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize, @RequestParam(name = "current", defaultValue = "0") int current) {
        AuthConstant.setPathUrl(uploadhost);
        Result result = new Result();
        MyPageOld list = masterService.findByHomePageList(pageSize, current);
        if (list.getSize() > 0) {
            result.setStatus(ResultConstant.SUCSESS);
            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }
        return result;
    }

    @IsLogin
    @GetMapping("/info")
    public Result info(@RequestParam(name = "id", defaultValue = "0") long id) {
        AuthConstant.setPathUrl(uploadhost);
        Result result = new Result();
        Map<String, Object> list = masterService.getInfo(id);
        if (!list.isEmpty()) {
            result.setStatus(ResultConstant.SUCSESS);
            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }
        return result;
    }

    @IsLogin
    @GetMapping("/likeOrView")
    public Result likeOrView(@RequestParam(name = "detailId", defaultValue = "0") long detailId,
                             @RequestParam(name = "masterId", defaultValue = "0") long masterId,
                             @RequestParam(name = "type", defaultValue = "0") int type) {
        boolean b = false;
        if (type == 1) {
            b = masterService.like(detailId, masterId);
        } else {
            b = masterService.view(detailId, masterId);
        }

        return b ? Result.ok() : Result.failure();
    }

}
